$(function () {
    var userName;
    $(".i_car .price_a a").click(function () {
        if (localStorage.length==0) {
            if(confirm("请先登录")){
                window.location.href = "Login.html";
            }
        }
    });
    if (localStorage.length != 0) {
        userName = localStorage.getItem("user");
        $.getJSON("easybuy/Car/selectCars",
            {"userName":userName},
            function (data) {
                var sumPrice = 0;
                $(".i_car").html("<div class=\"car_t\">购物车 [ <span>"+data.length+"</span> ]</div>\n" +
                    "        <div class=\"car_bg\">\n" +
                    "       \t\t<!--Begin 购物车未登录 Begin-->\n" +
                    "            <!--End 购物车未登录 End-->\n" +
                    "            <!--Begin 购物车已登录 Begin-->\n" +
                    "            <ul class=\"cars\">\n" +
                    "            \t\n" +
                    "            </ul>\n" +

                    "            <!--End 购物车已登录 End-->\n" +
                    "        </div>");
                for (var i = 0; i < data.length; i++) {
                    $(".i_car .cars").append("<li>\n" +
                        "                \t<div class=\"img\"><a href=\"#\"><img src=\"images/"+data[i].fileName+"\" width=\"58\" height=\"58\" /></a></div>\n" +
                        "                    <div class=\"name\"><a href=\"#\">"+data[i].name+"</a></div>\n" +
                        "                    <div class=\"price\"><font color=\"#ff4e00\">￥"+data[i].price+"</font> X"+data[i].count+"</div>\n" +
                        "                </li>");
                    sumPrice = sumPrice+data[i].price*data[i].count;
                }
                $(".i_car .car_bg").append("<div class=\"price_sum\">共计&nbsp; <font color=\"#ff4e00\">￥</font><span>"+sumPrice+"</span></div>\n" +
                    "<div class=\"price_a\"><a href=\"BuyCar.html\">去购物车结算</a></div>");
            });
    }

    $(".carbg a:odd").live("click",function () {
        var id = $(this).parent().find(".id").val();
        if(localStorage.length == 0){
            if(confirm("请先登录")){
                window.location.href = "Login.html";
            }
        }else if(localStorage.length>0){
            userName = localStorage.getItem("user");
            $.ajax({
                url : "easybuy/Car/addCar",
                dataType : "json",
                data : {"userName":userName,"id" : id,"count":1},
                type : "get",
                success : function (data) {
                    if(data){
                        alert("加入购物车成功");
                        $.getJSON("easybuy/Car/selectCars",
                            {"userName":userName},
                            function (data) {
                                var sumPrice = 0;
                                $(".i_car").html("<div class=\"car_t\">购物车 [ <span>"+data.length+"</span> ]</div>\n" +
                                    "        <div class=\"car_bg\">\n" +
                                    "       \t\t<!--Begin 购物车未登录 Begin-->\n" +
                                    "            <!--End 购物车未登录 End-->\n" +
                                    "            <!--Begin 购物车已登录 Begin-->\n" +
                                    "            <ul class=\"cars\">\n" +
                                    "            \t\n" +
                                    "            </ul>\n" +

                                    "            <!--End 购物车已登录 End-->\n" +
                                    "        </div>");
                                for (var i = 0; i < data.length; i++) {
                                    $(".i_car .cars").append("<li>\n" +
                                        "                \t<div class=\"img\"><a href=\"#\"><img src=\"images/"+data[i].fileName+"\" width=\"58\" height=\"58\" /></a></div>\n" +
                                        "                    <div class=\"name\"><a href=\"#\">"+data[i].name+"</a></div>\n" +
                                        "                    <div class=\"price\"><font color=\"#ff4e00\">￥"+data[i].price+"</font> X"+data[i].count+"</div>\n" +
                                        "                </li>");
                                    sumPrice = sumPrice+data[i].price*data[i].count;
                                }
                                $(".i_car .car_bg").append("<div class=\"price_sum\">共计&nbsp; <font color=\"#ff4e00\">￥</font><span>"+sumPrice+"</span></div>\n" +
                                    "<div class=\"price_a\"><a href=\"BuyCar.html\">去购物车结算</a></div>");
                            });
                    }else{
                        alert("超出库存！加入购物车失败");
                    }
                },
                error : function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("出现错误");
                    console.log(XMLHttpRequest.status);
                    console.log(XMLHttpRequest.readyState);
                    console.log(textStatus);
                }
            });


        }

    });


});